/**
 * 
 */
package uk.ac.lkl.lpp.ui;


//import java.awt.LayoutManager;
//import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;
import javax.swing.SpringLayout;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import uk.ac.lkl.common.ui.SpringUtilities;
import uk.ac.lkl.common.util.ds.NotifyingList;
import uk.ac.lkl.lpp.model.ldo.LearningEntity;

/**
 * @author yish
 *
 */
public final class LEPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	
	public static final String EDIT_BUTTON_KEY   = "editButton";
	public static final String DEL_BUTTON_KEY    = "delButton";
	public static final String OK_BUTTON_KEY     = "okButton";
	public static final String CANCEL_BUTTON_KEY = "cancelButton";

	
	private LearningEntity le;
	private NotifyingList list;

	private JPanel  display = new JPanel(new GridBagLayout());//new SpringLayout());// 
	private EntityNameButton titleDisplay       = new EntityNameButton();
	private JLabel  descriptionDisplay = new JLabel();

	private JPanel     editor  = new JPanel(new GridBagLayout());
	private JTextField titleEdit = new JTextField(10);
	private JTextField linkEdit = new JTextField(10);	
	private JTextArea  descriptionEdit = new JTextArea(3,20);
	
	private GridBagConstraints gc = new GridBagConstraints ();  

	/**
	 * 
	 */
	public LEPanel(LearningEntity le, NotifyingList list) {
		setLayout(new GridLayout(1,1));
		this.list = list;
		setLE(le);
		buildDisplay();
		buildEdit();
		add(display);
	}
	
	public void setLE(LearningEntity le){
		this.le = le;
		titleDisplay.setEntity(le);
		updateDisplay();
		le.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e){
				updateDisplay();
			}			
		});
	}
	
	protected void updateDisplay(){
		// titleDisplay listens to changes in the entity.
//		String tmp = "";
//		if (le != null)  tmp = le.getTitle();
//		titleDisplay.setText(tmp);
		
//		if (le != null)  tmp = le.getDescription();
		descriptionDisplay.setText(le.getDescription());
	}

	protected void buildDisplay() {
		gc.ipadx = 2;
		gc.gridy = 0;
		gc.gridx = GridBagConstraints.LINE_START;
		gc.weightx = 0.0;
		display.add(titleDisplay,gc);
		gc.gridx = GridBagConstraints.RELATIVE;
		gc.weightx = 1.0;
		gc.weighty = 1.0;
		gc.fill = GridBagConstraints.BOTH;
		display.add(descriptionDisplay,gc);
		gc.fill = GridBagConstraints.NONE;
		JButton eb = Messages.getJButton(EDIT_BUTTON_KEY);
		eb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				startEdit();
			}
		});
		gc.weightx = 0.0;
		gc.weighty = 0.0;
		display.add(eb,gc);
		JButton db = Messages.getJButton(DEL_BUTTON_KEY);
		db.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				delete();
			}
		});
		gc.weightx = 0.0;
		display.add(db,gc);
//		SpringUtilities.makeCompactGrid(display,1,4,5,5,5,5);
		
	}

	/**
	 * 
	 * <code>
	 * ___________________________________
	 * | [ title        ] T description  T [ok]
	 * | http:// [ link ] L              J [cancel]
	 * ----------------------------------- 
	 * </code>
	 */
	protected void buildEdit() {
		// title 
		gc.gridy = 0;
		gc.gridx = GridBagConstraints.LINE_START;
		gc.weightx = 0.3;
		gc.gridwidth = 2;
		gc.fill = GridBagConstraints.BOTH;
		editor.add(titleEdit,gc);
		
		// link
		gc.weightx = 0.0;
		gc.gridy = 1;
		gc.gridwidth = 1;
		editor.add(new JLabel("http://"),gc);
		gc.gridx = GridBagConstraints.RELATIVE;
		editor.add(linkEdit,gc);
		
		// description
		gc.gridy = 0;
		gc.gridx = GridBagConstraints.RELATIVE;
		gc.weightx = 0.6;
		gc.gridheight = 2;
		descriptionEdit.setBorder(BorderFactory.createEtchedBorder());
		editor.add(descriptionEdit,gc);
		
		// ok
		JButton okb = Messages.getJButton(OK_BUTTON_KEY);
		okb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				editOk();
			}
		});
		gc.gridheight = 1;
		gc.weightx = 0.0;
		gc.fill = GridBagConstraints.NONE;
		editor.add(okb,gc);
		
		// cancel
		JButton cb = Messages.getJButton(CANCEL_BUTTON_KEY);
		cb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				editEnd();
			}
		});
		gc.gridy = 1;
		editor.add(cb,gc);

	}

	
	protected void startEdit() {
		titleEdit.setText(le.getTitle());
		titleEdit.setSize(titleEdit.getPreferredSize());
		descriptionEdit.setText(le.getDescription());
		remove(display);
		add(editor);
		validate();
	}
	
	protected void delete() {
		list.remove(le);
	}
	
	protected void editOk() {
		le.setTitle(titleEdit.getText());
		le.setDescription(descriptionEdit.getText());
		String link = linkEdit.getText();
		if (link != null && link.length() > 6) {
			le.setLink("http://"+link);
		}
		editEnd();
	}
	
	protected void editEnd() {
		remove(editor);
		add(display);
		validate();
	}

}
